﻿/*
 * Copyright (C) 2012, Jesus Requejo <jesus.requejo@gmail.com> 
 * Licensed under Microsoft Public License (Ms-PL)
 * http://www.microsoft.com/en-us/openness/licenses.aspx#MPL
*/

namespace LoaderServiceTray {

   using System;
   using System.Threading;
   using System.Windows.Forms;
   using Infrastructure;

   static class Program {

      private static string appGuid = "A97C2650-1933-4F13-B2A8-D0C94D378D96";

      [STAThread]
      static void Main() {

         using (Mutex mutex = new Mutex(false, appGuid)) {
            if (!mutex.WaitOne(0, false)) {
               // see http://sanity-free.org/143/csharp_dotnet_single_instance_application.html for the original inspiration
               // he doesn't deal with the problem where minimized apps can't receives messages though
               BringRunningInstanceToFront();
               return;
            }

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
         }
      }

      private static void BringRunningInstanceToFront() {
         string windowName = MagicStrings.MonitorMessageWindowName;
         string message = MagicStrings.BringServiceMonitorToFrontMessage;
         Win32WindowMessengerWrapper.SendMessageToWindow(windowName, message);
      }
   }
}

